#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    // socket->connectToHost("192.168.12.72",6666);
    socket = nullptr;

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    QByteArray data = ui->lineEdit->text().toLatin1();
    socket->write(data);
}
void Widget::recv_data(){
    QByteArray data = socket->readAll();
    ui->textEdit->setPlainText(data);
}
void Widget::cnd_slot(){
    qDebug()<<"cnd sucess";
}

void Widget::on_pushButton_2_clicked()
{
    if(socket ==nullptr){
        socket  = new QTcpSocket(this);
        socket->connectToHost(QHostAddress::LocalHost,8888);
        connect(socket,SIGNAL(connected()),this,SLOT(cnd_slot()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(recv_data()));
    }else {
        socket->connectToHost(QHostAddress::LocalHost,8888);
        connect(socket,SIGNAL(connected()),this,SLOT(cnd_slot()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(recv_data()));
    }


}
